/*
 * StringUtilities.java
 *
 * Created on 2005/01/23 +0900
 *
 * $Revision: 40 $
 * $Date: 2008-10-26 13:45:24 +0000 (Sun, 26 Oct 2008) $
 */

package org.gikolet.framework.util;

import java.util.Vector;

/**
 * @author Tetsutaro (latest modification by $Author: tetsutaro.ss $)
 * @version $Revision: 40 $ $Date: 2008-10-26 13:45:24 +0000 (Sun, 26 Oct 2008) $
 */
public class StringUtilities {
    private static final String KANA_HALF = "ｱｲｳｴｵｧｨｩｪｫｶｷｸｹｺｻｼｽｾｿﾀﾁﾂｯﾃﾄﾅﾆﾇﾈﾉﾊﾋﾌﾍﾎﾏﾐﾑﾒﾓﾔﾕﾖｬｭｮﾗﾘﾙﾚﾛﾜｦﾝｰﾞﾟ､｡";
    private static final String KANA = "アイウエオァィゥェォカキクケコサシスセソタチツッテトナニヌネノハヒフヘホマミムメモヤユヨャュョラリルレロワヲンー゛゜、。";

    private static final String ALPHA = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ:/.";
    private static final String ALPHA_FULL = "ａｂｃｄｅｆｇｈｉｊｋｌｍｎｏｐｑｒｓｔｕｖｗｘｙｚＡＢＣＤＥＦＧＨＩＪＫＬＭＮＯＰＱＲＳＴＵＶＷＸＹＺ：／．";

    public static final String EMPTY_STRING = "";

    private StringUtilities() {
    }

    public static String[] tokenToStrings(String value, String delimiter) {
	return tokenToStrings(value, 0, value.length(), delimiter);
    }

    public static String[] tokenToStrings(String value, int offset, int count,
	    String delimiter) {
	Vector w = new Vector();

	int maxEnd = offset + count;

	int start = offset;
	int end;

	do {
	    end = value.indexOf(delimiter, start);
	    end = (end > maxEnd || end == -1) ? maxEnd : end;

	    w.addElement(value.substring(start, end));

	    start = end + delimiter.length();
	} while (start <= maxEnd);

	String[] strs = new String[w.size()];
	w.copyInto(strs);
	return strs;
    }

    public static String replace(String str, String before, String after) {
	StringBuffer sb = new StringBuffer();
	int start = 0;

	while (start < str.length()) {
	    int to = str.indexOf(before, start);
	    if (to == -1) {
		to = str.length();
		sb.append(str.substring(start, to));
	    } else {
		sb.append(str.substring(start, to));
		sb.append(after);
	    }
	    start = to + before.length();
	}
	return sb.toString();
    }

    public static String escape(String str, boolean ht, boolean lf) {
	StringBuffer sb = new StringBuffer(str);

	for (int i = 0; i < sb.length(); i++) {
	    char c = sb.charAt(i);

	    if (c == '\t') {
		if (ht) {
		    sb.deleteCharAt(i);
		    sb.insert(i, "\\t");
		    i++;
		}
	    } else if (c == '\n') {
		if (lf) {
		    sb.deleteCharAt(i);
		    sb.insert(i, "\\n");
		    i++;
		}
	    } else if (c == '\\') {
		int bsCount = 1;
		while (i + bsCount < sb.length() && sb.charAt(i + bsCount) == '\\') {
		    bsCount++;
		}
		if (i + bsCount < sb.length()) {
		    char c2 = sb.charAt(i + bsCount);
		    if ((ht && (c2 == '\t' || c2 == 't'))
			    || (lf && (c2 == '\n' || c2 == 'n'))) {
			for (int j = 0; j < bsCount; j++) {
			    sb.insert(i, '\\');
			}
			if ((ht && c2 == '\t') || (lf && c2 == '\n')) {
			    i--;
			}
			bsCount *= 2;
		    }
		}
		i += bsCount;
	    }
	}
	return sb.toString();
    }

    public static String unescape(String str, boolean ht, boolean lf) {
	StringBuffer sb = new StringBuffer(str);

	for (int i = 0; i < sb.length(); i++) {
	    char c = sb.charAt(i);

	    if (c == '\\') {
		int bsCount = 1;
		while (i + bsCount < sb.length() && sb.charAt(i + bsCount) == '\\') {
		    bsCount++;
		}
		if (i + bsCount < sb.length()) {
		    char c2 = sb.charAt(i + bsCount);
		    if ((ht && c2 == 't') || (lf && c2 == 'n')) {
			if (bsCount % 2 == 0) {
			    sb.delete(i, i + bsCount / 2);
			} else {
			    sb.delete(i + bsCount / 2, i + bsCount + 1);
			    if (c2 == 't') {
				sb.insert(i + bsCount / 2, '\t');
			    } else if (c2 == 'n') {
				sb.insert(i + bsCount / 2, '\n');
			    }
			}
			bsCount /= 2;
		    }
		}
		i = i + bsCount;
	    }
	}
	return sb.toString();
    }

    public static String toAlphaHalf(String str) {
	StringBuffer strBuffer = new StringBuffer();
	for (int i = 0; i < str.length(); i++) {
	    strBuffer.append(toAlphaHalf(str.charAt(i)));
	}
	return strBuffer.toString();
    }

    public static char toAlphaHalf(char kana) {
	int index;

	if ((index = ALPHA_FULL.indexOf(kana)) >= 0) {
	    kana = ALPHA.charAt(index);
	}
	return kana;
    }

    public static String toKanaFull(String str) {
	StringBuffer strBuffer = new StringBuffer();

	for (int i = 0; i < str.length(); i++) {
	    char c = toKanaFull(str.charAt(i));
	    if (c == '゛') {
		c = strBuffer.charAt(strBuffer.length() - 1);
		c++;
		strBuffer.deleteCharAt(strBuffer.length() - 1);
	    } else if (c == '゜') {
		c = strBuffer.charAt(strBuffer.length() - 1);
		c += 2;
		strBuffer.deleteCharAt(strBuffer.length() - 1);
	    }
	    strBuffer.append(c);
	}

	return strBuffer.toString();
    }

    public static char toKanaFull(char kana) {
	int index;

	if ((index = KANA_HALF.indexOf(kana)) >= 0) {
	    kana = KANA.charAt(index);
	}
	return kana;
    }
}
